home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / picture / camera.c < prev    next >
Text File  |  1993-09-23  |  2KB  |  83 lines

  1. //    Copyright 1993 Ralph Gonzalez
  2.  
  3. /*
  4. *    FILE:        camera.c
  5. *    AUTHOR:        R. Gonzalez
  6. *    CREATED:    October 3, 1990
  7. *
  8. *    Methods for camera class, which defines the view location.
  9. */
  10.  
  11. # include    "camera.h"
  12.  
  13. /******************************************************************
  14. *    initialize
  15. ******************************************************************/
  16. Camera::Camera(void)
  17. {
  18.     world_to_camera = new World_To_Camera;
  19.     
  20.     set_position(0.,0.,0.,0.,0.,0.);
  21.     set_focal_length(1.);
  22. }
  23.  
  24. /******************************************************************
  25. *    set location and orientation of camera using world coordinates.
  26. *    See camera.h for description.
  27. ******************************************************************/
  28. void    Camera::set_position(double x,double y,double z,
  29.                     double yaw,double pitch,double roll)
  30. {
  31.     world_to_camera->set(x,y,z,yaw,pitch,roll);
  32. }
  33.  
  34. /******************************************************************
  35. *    set focal length of camera. This can be used for "zoom" effect.
  36. *    Represents distance from camera origin to focal plane.
  37. ******************************************************************/
  38. void    Camera::set_focal_length(double f)
  39. {
  40.     focal_length = f;
  41. }
  42.  
  43. /******************************************************************
  44. *    convert 3D world coordinate to 2D image on focal plane.
  45. *    Returns FALSE if thing is in back of camera.
  46. ******************************************************************/
  47. boolean    Camera::take_photo(Coord2 *image,Coord3 *thing)
  48. {
  49.     boolean    success = TRUE;
  50.     Coord3    *temp;
  51.     
  52.     temp = new Coord3;
  53.     temp->apply(thing,world_to_camera);
  54.     
  55.     if (temp->z < 1e-4 && temp->z > -1e-4)
  56.     {
  57.         image->x = temp->x * focal_length;
  58.         image->x /= (temp->z+1e-4);    // broke up this expression because Think C choked
  59.         image->y = temp->y * focal_length;
  60.         image->y /= (temp->z+1e-4);
  61.     }
  62.     else
  63.     {
  64.         image->x = temp->x * focal_length/temp->z;
  65.         image->y = temp->y * focal_length/temp->z;
  66.     }
  67.     
  68.     if (temp->z < 1e-4)
  69.         success = FALSE;
  70.         
  71.     delete temp;
  72.     
  73.     return success;
  74. }
  75.  
  76. /******************************************************************
  77. *    destroy
  78. ******************************************************************/
  79. Camera::~Camera(void)
  80. {
  81.     delete world_to_camera;
  82. }
  83.